package ch8;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.*;

public class MySocket2 {

	public static void main(String[] args) {
		try {
			ServerSocket st = new ServerSocket(6666);
			System.out.println("服务端启用成功！");
			while (true) {
				Socket s = null;
				try {
					s = st.accept();// 接收
					System.out.println("本地：" + s.getLocalAddress() + "端口：" + s.getLocalPort());
					System.out.println("远程：" + s.getInetAddress() + ",端口：" + s.getPort());
					InputStream in = s.getInputStream();
					// OutputStream out = s.getOutputStream();
					BufferedReader br = new BufferedReader(new InputStreamReader(in));
					String str = null;
					while ((str = br.readLine()) != null) {
						System.out.println(str);
					}

				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					try {
						if (s != null)
							s.close();
					} catch (Exception e) {
						// TODO 自动生成的 catch 块
						e.printStackTrace();
					}
				}
			}
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}

	}

}
